package com.kgc.service.impl;

import com.kgc.dao.SmbmsDao;
import com.kgc.entity.Provider;
import com.kgc.entity.Role;
import com.kgc.entity.User;
import com.kgc.service.SmbmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SmbmsServiceImpl implements SmbmsService {


    @Autowired
    private SmbmsDao dao;


    @Transactional(readOnly = true)
    @Override
    public User getUserByusernameAndpassword(String username) {

        return dao.getUserByusernameAndpassword(username);
    }


    @Transactional(readOnly = true)
    @Override
    public List<User> listUser(String name, String userRole, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        if (name != null && !"".equals(name)) {
            map.put("username", name);
        }
        if (!"0".equals(userRole)) {
            map.put("userRole", userRole);
        }
        map.put("start", (currentPage - 1) * 4);

        return dao.listUser(map);
    }


    @Transactional(readOnly = true)
    @Override
    public List<Role> listRolename() {

        return dao.listRolename();
    }


    @Transactional(readOnly = true)
    @Override
    public int countUsers(String name, String userRole) {
        Map<String, Object> map = new HashMap<>();
        if (name != null && !"".equals(name)) {
            map.put("username", name);
        }
        if (!"0".equals(userRole)) {
            map.put("userRole", userRole);
        }

        return dao.countUsers(map);


    }


    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int insertUser(User user) {

        return dao.insertUser(user);
    }


    @Transactional(readOnly = true)
    @Override
    public User selectUser(int id) {

        return dao.selectUser(id);
    }


    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int deleteUser(int id) {

        return dao.deleteUser(id);
    }


    @Transactional(readOnly = true)
    @Override
    public List<Provider> listProviders(Map<String,Object> paramMap) {

        return dao.listProviders(paramMap);
    }

    @Transactional(readOnly = true)
    @Override
    public int countUserByUsercode(String usercode) {
        return dao.countUserByUsercode(usercode);
    }
}
